/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Noel De Palma - initial API and implementation
 ******************************************************************************/
/***
 * FractalLauncher
 * Launching of a Fractal generic factory component on the local host 
 * Created 07/06/04
 * @contact: Fabienne.Boyer@imag.fr
 * @author: fabienne Boyer
 */

package fr.jade.service.nodes.factory;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Fractal;
import org.objectweb.fractal.api.factory.GenericFactory;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;

import fr.jade.fraclite.orb.registry.NamingService;
import fr.jade.fraclite.orb.registry.Registry;
import fr.jade.reflex.api.control.ReflexController;
import fr.jade.reflex.util.Reflex;
import fr.jade.util.FractalUtil;
import fr.jade.util.log.Logger;
import fr.jade.util.log.config.DebugService;



/**
 * Launch a new JadeNode.
 * 
 * @author Noel De Palma
 * @author Sylvain Sicard
 * @contact sylvain.sicard@gmail.com
 */
public class NodeLauncher {
	
	public static void main (final String[] args) throws Exception {
	  assert args.length == 1 : "Usage: NodeLauncher <nodename>,<jadenodename>,<jadeport>";	
	  String[] param = args[0].split(",");
	  assert param.length == 3 : "Usage: NodeLauncher <nodename>,<jadenodename>,<jadeport>";
	  
	  String nodeName = param[0];
	  String registryNode = param[1];
		int    registryPort = Integer.parseInt(param[2]);
		
    // create exec bootstrap component
    Logger.println("Registry properties " + registryNode);
    Component boot = Fractal.getBootstrapComponent();
    GenericFactory gf = (GenericFactory) boot.getFcInterface("generic-factory");
    TypeFactory tf = (TypeFactory) boot.getFcInterface("type-factory");
    Component node = gf.newFcInstance(tf.createFcType(new InterfaceType[]{}), "node", null);
    Reflex.getReflexController(node).setLevel(ReflexController.EXEC_LEVEL);
    FractalUtil.getPingController(node).start();
		NamingService ns = Registry.getRegistry(registryNode, registryPort);
		ns.bind(nodeName, node);
		Logger.println(DebugService.on,"FractalLauncher: Fc bootstrapped on node " +  nodeName);
	}
	
}
